//
//  THCountdownManager.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THCountdownManager.h"

@interface THCountdownManager ()
//定时器结束时间戳
@property (nonatomic, assign) NSTimeInterval finishTimeInterval;
//定时器
@property (nonatomic, strong) NSTimer *activeTimer;
//定时器执行间隔
@property (nonatomic, assign) NSTimeInterval activeTimeInterval;

@end

@implementation THCountdownManager

+ (THCountdownManager *)startCountdownTimerWithDelayTime:(NSTimeInterval)delayTime countDownActive:(nullable THCountdownManagerBlock )countDownActive{
    THCountdownManager *countDown = [[THCountdownManager alloc] init];
    countDown.countDownActive = countDownActive;
    [countDown startCountDownWithDelayTime:delayTime];
    return countDown;
}

+ (THCountdownManager *)startCountdownTimerWithFinishTimeInterval:(NSTimeInterval)finishTimeInterval countDownActive:(nullable THCountdownManagerBlock)countDownActive{
    THCountdownManager *countDown = [[THCountdownManager alloc] init];
    countDown.countDownActive = countDownActive;
    [countDown startCountDownWithTimerInterval:finishTimeInterval];
    return countDown;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.activeTimeInterval = 1;
    }
    return self;
}
//重新设置 定时器间隔,必须删除已有的定时器,重新创建
- (void)resetActiveTimeInterval:(NSTimeInterval )activeTimeInterval{
    if (self.activeTimeInterval == activeTimeInterval) {
        return;
    }
    self.activeTimeInterval = activeTimeInterval;
    if (_activeTimer) {//如果定时器再跑
        [self endTimer];//先停止
        [self startTimer];//再创建一个新的定时器
    }
}
- (void)refreshTimerWithTimerInterval:(NSTimeInterval )timerInterval{
    self.finishTimeInterval = timerInterval;
}
- (void)refreshTimerWithDelayTime:(NSTimeInterval )delayTime{
    self.finishTimeInterval = delayTime+[self getNowTimeInterval];
}
#pragma mark Public
#pragma mark 开启倒计时
- (void)startCountDownWithTimerInterval:(NSTimeInterval )timerInterval{
    self.finishTimeInterval = timerInterval;
    if (self.countDownStarted) {
        self.countDownStarted(roundf(self.finishTimeInterval - [self getNowTimeInterval]));
    }
    [self startTimer];
}
- (void)startCountDownWithDelayTime:(NSTimeInterval )delayTime{
    self.finishTimeInterval = delayTime + [self getNowTimeInterval];
    if (self.countDownStarted) {
        self.countDownStarted(roundf(self.finishTimeInterval - [self getNowTimeInterval]));
    }
    [self startTimer];
}
- (void)startCountDown{
    self.finishTimeInterval = LONG_MAX +[self getNowTimeInterval];
    if (self.countDownStarted) {
        self.countDownStarted(self.finishTimeInterval - [self getNowTimeInterval]);
    }
    [self startTimer];
}

#pragma mark 结束倒计时
- (void)stopCountDown{
    [self endTimer];
    if (self.countDownEnded) {
        self.countDownEnded(self.finishTimeInterval - [self getNowTimeInterval]);
    }
}

#pragma mark Private
- (void)startTimer{
    if (!_activeTimer) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSRunLoop currentRunLoop] addTimer:self.activeTimer forMode:NSRunLoopCommonModes];
        });
    }
}
- (void)endTimer{
    if (_activeTimer) {
        [_activeTimer invalidate];
        _activeTimer = nil;
    }
}

#pragma mark 获取时间戳
- (NSTimeInterval)getNowTimeInterval{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [dat timeIntervalSince1970];
    return a;
}

#pragma mark 定时器执行方法
- (void)timerActive{
    NSTimeInterval remainingTime = self.finishTimeInterval - [self getNowTimeInterval];
    if (self.countDownActive) {
        self.countDownActive(remainingTime);
    }
    if (remainingTime < 0) {
        [self stopCountDown];
    }
}
- (NSTimer *)activeTimer{
    if (!_activeTimer) {
        _activeTimer = [NSTimer timerWithTimeInterval:self.activeTimeInterval target:self selector:@selector(timerActive) userInfo:nil repeats:YES];
    }
    return _activeTimer;
}

/// 定时器是否正在执行
- (BOOL)isRunning
{
    return _activeTimer && _activeTimer.isValid;
}

- (void)dealloc{
//    NSLog(@"释放THCountdownManager");
}


@end
